<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Control extends Controller 
{
	function __construct()
	{
		parent::Controller();
        $this->auth->CheckTaMeR(100,2);
        $this->load->model('Tamer_Model', 'tm');
		$this->setVars();
        //$this->load->library('profiler');
        log_message('debug', "Control Panel->Control: Controler Class Initialized");
	}
    
	private function setVars()
	{
		$this->tpl['title'] = "Control Panel";
        $this->tpl['sidebar'] = $this->tm->randomBlock();
		$this->tpl['head'] = '';
        $this->tpl['body'] = '';
        
        $this->db_table = 'contact_user';
        $this->uid = $this->tm->get_uid();
        
        define('URI',$this->uri->segment(1).'/'.$this->uri->segment(2));
	}
	
    
    /**
    * @note	index
    * @info	Just forwarding
    * @author	Dennis T. Kaplan
    * @access	public
    * @param	 
    * @return	html page
    */	
    public function index()
    {
        if($this->input->post('save') !== FALSE)
        {
            if($this->validate() === TRUE)
            {
            	$this->save();
            }
        }
        $this->formTimezone();
    }
    
    /**
    * @note	form
    * @info	Create Form in Table 
    * @author	Dennis T. Kaplan
    * @access	public
    * @param	 
    * @return	html
    */	
    private function formTimezone()
    {
        $sql = "
        SELECT places_timezone.id, FIPS, TZ_Linux, Country 
        FROM places_timezone
        JOIN (places_country) 
        ON (places_timezone.ISO3166 = places_country.ISO3166)
        ";
		$query = $this->db->query($sql);
		foreach ($query->result() as $row)
		{
            $l = strstr($row->TZ_Linux, '/');
            $c = $row->Country;
            $t[$row->TZ_Linux] = $c.$l;
		}
        asort($t);
		$output = '<select name="timezone">';
        $output .= "<option value=\"\">--Location by Timezone--</option>\n";
        $this->getTimezone();
        foreach($t as $k=>$v){
            $sel = ($k != $this->db_tz) ? '' : ' selected="selected"';
            $output .= "<option value=\"$k\" $sel>$v</option>\n";
        }
		$output .= '</select>';
        $this->TZ_Linux = $row->TZ_Linux;
        
        $form['Timezone'] = $output;
        $form[''] = form_submit(array(
        'name'        => 'save',
        'class'       => 'submit',
        'value'       => 'Save',
        'style'       => 'margin:10px'
        )
        );
        $this->load->library('table');
        $this->tpl['body'] .= form_open(URI, array('name' => 'TimeZoneForm'));
        $this->tpl['body'] .= $this->table->form_table($form);
        $this->tpl['body'] .= '</form>';
        $this->load->view('blank', $this->tpl);
    }
    
    /**
    * @note	validate
    * @info	Validate submited form
    * @author	Dennis T. Kaplan
    * @access	private
    * @param	 
    * @return	TRUE || FALSE
    */	
    private function validate()
    {
        log_message('debug', 'Company: Validation initiated');
        $this->load->library('validation');
		$rules['timezone'] = "trim|required|xss_clean";
        
        $this->validation->set_rules($rules);
        if($this->validation->run() !== FALSE){
            $this->getTimezone();
            if($this->validation->timezone != $this->db_tz){
                $this->msg->setMsg($this->validation->timezone);
                $this->msg->setMsg($this->db_tz);
                
                $this->save();
            }else{
                $this->msg->setError('Not Updated');
                $this->msg->setMsg('You have not made any changes.');
            }
        }
    }
    
    private function save()
    {
            log_message('info', 'update uid: '. $this->uid.' TZ= '.$this->validation->timezone);
            $this->db->where('uid', $this->uid);
            $this->db->update($this->db_table, array('timezone'=>$this->validation->timezone));
            $this->session->set_userdata(array('TZ'=>$this->validation->timezone));
            del_cookie('TZ');
            set_cookie('TZ',$this->validation->timezone,60);
            $this->msg->setMsg('Your record has been updated');
    }

    /**
         * @note	det timezone from db
         * @info	
         * @author	Dennis T. Kaplan
         * @access	private
         * @param	 
         * @return	string
         */	
        private function getTimezone()
        {
            if(!isset($this->db_tz)){
                $this->db->select('timezone');
                $this->db->where('uid', $this->uid);
                $query = $this->db->get($this->db_table);
                
                if ($query->num_rows() > 0) {
                    $row = $query->row();
                    $this->db_tz = $row->timezone;
                } 
            }
        }    
}

?>
